package pl.koziolekweb.szkola.LASerialNet;

// LA-SerialNet/Rysuj.java
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

public class Rysuj extends JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
        new Rysuj();
    }

    Rysuj() {
        super("Klient");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        final MyPanel panel = new MyPanel();
        panel.addMouseListener(panel);
        panel.addMouseMotionListener(panel);
        panel.addKeyListener(panel);

        panel.setBackground(Color.BLACK);
        panel.setOpaque(true);
        panel.setPreferredSize(new Dimension(300,200));

        JButton send = new JButton(
            new AbstractAction("Wy\u015blij") {
                /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent e) {
                    Socket soc = null;
                    ObjectOutputStream out = null;
                    try {
                        soc = new Socket("localhost",54321);
                        out = new ObjectOutputStream(
                                  new BufferedOutputStream(
                                  soc.getOutputStream()));
                        out.writeObject(panel.kreski);
                        out.close();
                        panel.requestFocus();
                    }catch(Exception a) {
                        a.printStackTrace();
                        System.exit(1);
                    }
                    finally {
                        try {
                            if (soc != null) soc.close();
                        } catch(IOException ignore) { }
                    }
                }
            }
        );

        add(panel,BorderLayout.CENTER);
        add(send, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }
}
